package top.fullj.actor;

/**
 * Actor's path
 *
 * @author wurenhai
 * @since 2018/3/11
 */
public class ActorPath {

    /**
     * root actor name
     */
    public static final String ROOT = "/";

    /**
     * usr actor name
     */
    public static final String USR = "usr";

    /**
     * sys actor name
     */
    public static final String SYS = "sys";

    /**
     * highest level system actor name
     */
    public static final String SYSTEM = "@";

    public static final String ANY = "*";

    //public static final String TREE = "**";

    /**
     * dead letters actor name
     */
    public static final String DEAD_LETTERS = "dlq";

    private final String parent;

    private final String value;

    public String name() {
        return value.substring(value.lastIndexOf("/") + 1);
    }

    public String value() {
        return value;
    }

    private ActorPath(String parent, String name) {
        this.parent = parent;
        if (isRoot(parent)) {
            value = parent + name;
        } else if (isRoot(name)) {
            value = name;
        } else {
            value = parent + "/" + name;
        }
    }

    ActorPath(String parent) {
        this.parent = parent;
        this.value = parent;
    }

    public boolean isRoot() {
        return parent.equals(SYSTEM) && value.equals(ROOT);
    }

    private boolean isRoot(String path) {
        return ROOT.equals(path);
    }

    /**
     * new child ActorPath of the path
     */
    public ActorPath withName(String name) {
        return new ActorPath(value, name);
    }

    @Override
    public String toString() {
        return value;
    }

}
